package com.czk.doc;

import com.czk.util.ClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;

/**
 * 带条件（全量）的查询
 *
 * @Author:ChenZhangKun
 * @Date: 2021/6/14 17:00
 */
public class AllMatchQuery {
    public static void main(String[] args) throws IOException {
        // 客户端
        RestHighLevelClient highLevelClient = ClientUtil.getHighLevelClient();
        // 插寻
        SearchRequest searchRequest = new SearchRequest();
        // 设置索引库
        searchRequest.indices("user");
        // 构建查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 增加查询体
        // 查询全部数据
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        searchRequest.source(searchSourceBuilder);
        // 查询
        SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        // 拿到全部数据条数
        SearchHits hits = response.getHits();
        // 条数
        long hits1 = hits.getHits().length;
        // 时间
        System.out.println(response.getTook());
        // 虚幻
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit.getSourceAsString());
        }
    }
}
